package huaweiod.first;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 华为OD机试真题 Java 实现【数组合并】【2022.11 Q4 新题】
 */
public class A_2023_34_ArrayUnion {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int count = sc.nextInt();
        sc.nextLine();
        List<LinkedList<Integer>> collect = new LinkedList<>();
        for (int i = 0; i < count; i++) {
            LinkedList<Integer> list = Arrays.stream(sc.nextLine().split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toCollection(LinkedList::new));
            collect.add(list);
        }
        StringJoiner sj = new StringJoiner(",");
        while (!collect.isEmpty()) {
            for (int i = 0; i < collect.size(); i++) {
                LinkedList<Integer> list = collect.get(i);
                int min = Math.min(list.size(), len);
                for (int j = 0; j < min; j++) {
                    sj.add(String.valueOf(list.get(0)));
                    list.remove(0);
                }
                if (list.size() == 0) {
                    collect.remove(i--);
                }
            }
        }
        System.out.println(sj.toString());
    }
}
